$(document).ready(function() {
    $('#print-btn').DataTable({
        dom: 'Bfrtip',
        buttons: ['print']
    });
    $('#custom-print-msg').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'print',
            message: 'List of total order in this month by Flat - Admin'
        }]
    });
    $('#print-disable').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'print',
            autoPrint: false
        }]
    });
    $('#print-column').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'print',
            exportOptions: {
                columns: ':visible'
            }
        }, 'colvis'],
        columnDefs: [{
            targets: -1,
            visible: false
        }]
    });
    $('#print-row').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'print',
            text: 'Print all'
        }, {
            extend: 'print',
            text: 'Print selected',
            exportOptions: {
                modifier: {
                    selected: true
                }
            }
        }],
        select: true
    });
    $('#flash-export').DataTable({
        dom: 'Bfrtip',
        buttons: ['copyFlash', 'csvFlash', 'excelFlash', 'pdfFlash']
    });
    $('#file-export').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'excelFlash',
            filename: 'my-Excel'
        }, {
            extend: 'pdfFlash',
            filename: 'my-PDF'
        }]
    });
    $('#flash-pdf-msg').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'pdfFlash',
            message: 'PDF created by Buttons for DataTables.'
        }]
    });
    $('#flash-size').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'pdfFlash',
            orientation: 'landscape',
            pageSize: 'LEGAL'
        }]
    });
    $('#cbtn-selectors').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'copyHtml5',
            exportOptions: {
                columns: [0, ':visible']
            }
        }, {
            extend: 'excelHtml5',
            exportOptions: {
                columns: ':visible'
            }
        }, {
            extend: 'pdfHtml5',
            exportOptions: {
                columns: [0, 1, 2, 5]
            }
        }, 'colvis']
    });
    $('#excel-bg').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'excelHtml5',
            customize: function(xlsx) {
                var sheet = xlsx.xl.worksheets['sheet1.xml'];
                $('row c[r^="F"]', sheet).each(function() {
                    if ($('is t', this).text().replace(/[^\d]/g, '') * 1 >= 500000) {
                        $(this).attr('s', '20');
                    }
                });
            }
        }]
    });
    $('#pdf-msg').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'pdfHtml5',
            message: 'PDF created by PDFMake with Buttons for DataTables.'
        }]
    });
    $('#pdf-size').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'pdfHtml5',
            orientation: 'landscape',
            pageSize: 'LEGAL'
        }]
    });
    $('#pdf-img').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'pdfHtml5',
            customize: function(doc) {
                doc.content.splice(1, 0, {
                    margin: [0, 0, 0, 12],
                    alignment: 'center',
                    image: ''
                });
            }
        }]
    });
    $('#pdf-open').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'pdfHtml5',
            download: 'open'
        }]
    });
    $('#pdf-json').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            text: 'JSON',
            action: function(e, dt, button, config) {
                var data = dt.buttons.exportData();
                $.fn.dataTable.fileSave(new Blob([JSON.stringify(data)]), 'Export.json');
            }
        }]
    });
    $('#basic-btn').DataTable({
        dom: 'Bfrtip',
        buttons: ['copy', 'csv', 'excel', 'pdf', 'print']
    });
    $('#custom-btn').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            text: 'My Custom button',
            action: function(e, dt, node, config) {
                alert('Button activated');
            }
        }]
    });
    $('#class-btn').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            text: 'Warning',
            className: 'btn-warning'
        }, {
            text: 'Danger',
            className: 'btn-danger'
        }, {
            text: 'Inverse',
            className: 'btn-inverse'
        }]
    });
    $('#keyboard-btn').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            text: 'Button <u>1</u>',
            key: '1',
            action: function(e, dt, node, config) {
                alert('Button 1 activated');
            }
        }, {
            text: 'Button <u><i>alt</i> 2</u>',
            key: {
                altKey: true,
                key: '2'
            },
            action: function(e, dt, node, config) {
                alert('Button 2 activated');
            }
        }]
    });
    $('#multilevel-btn').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'collection',
            text: 'Table control',
            buttons: [{
                text: 'Toggle start date',
                action: function(e, dt, node, config) {
                    dt.column(-2).visible(!dt.column(-2).visible());
                }
            }, {
                text: 'Toggle salary',
                action: function(e, dt, node, config) {
                    dt.column(-1).visible(!dt.column(-1).visible());
                }
            }, 'colvis']
        }]
    });
    var table = $('#multibtn-grup').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            text: 'Button 1',
            action: function(e, dt, node, config) {
                alert('Button 1 clicked on');
            }
        }]
    });
    // new $.fn.dataTable.Buttons(table, {
    //     buttons: [{
    //         text: 'Button 2',
    //         action: function(e, dt, node, conf) {
    //             alert('Button 2 clicked on');
    //         }
    //     }, {
    //         text: 'Button 3',
    //         action: function(e, dt, node, conf) {
    //             alert('Button 3 clicked on');
    //         }
    //     }]
    // });
    // table.buttons(1, null).container().appendTo(table.table().container());
    // $('#page-length').DataTable({
    //     dom: 'Bfrtip',
    //     lengthMenu: [
    //         [10, 25, 50, -1],
    //         ['10 rows', '25 rows', '50 rows', 'Show all']
    //     ],
    //     buttons: ['pageLength']
    // });
});
